<?php

/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

/*
 * template_parse_base 解析
 * @param $string foreach字符串
 * @return $string
 */

function template_parse_basevar($string) {
    $all_base = array();
    preg_match_all("/\{(\\\$[^\{]*?)\}/is", $string, $all_base);

    if (count($all_base[0]) == 0)
        return $string;

    $replacement = array();
    foreach ($all_base[1] as $k => $v) {
        $replacement[] = "<?php echo ".template_var_call_fun($v)." ; ?>";
    }


    /*
     * 改写基本变量解析函数   备份 是备份就不要乱删除了
      foreach ($all_base[1] as $k=>&$v) {
      $varr=explode("|", preg_replace("/\\\\\|/", "_suxian_", $v));
      $tempstring='<?php echo ';
      if(count($varr)==1){
      $tempstring.="$v ; ?>";
      }else{
      $count=count($varr);
      $thisvar="$varr[0]";
      for($i=1;$i<$count;$i++){
      $varr[$i]=preg_replace(array("/\\\:/","/\\\"/"), array("_mao_hao_","_shuang_yinhao_"), $varr[$i]);
      $varr[$i]=explode(":", $varr[$i]);
      foreach ($varr[$i] as &$value){
      $value=preg_replace(array("/_mao_hao_/","/_shuang_yinhao_/","/_suxian_/"),array(":","\"","|"),$value);
      }
      unset($value);
      $thisvartemp="".array_shift($varr[$i])."(";
      if(count($varr[$i])==0){
      $thisvartemp.=$thisvar.")";
      }else{
      $temps2=implode("," , $varr[$i]);
      if(preg_match_all("(,this)", $temps2, $matchtemp)){
      $temps2=preg_replace("/".$matchtemp[0][0]."/i", ",".$thisvar, $temps2);
      }
      if(preg_match_all("(this,)", $temps2, $matchtemp)){
      $temps2=preg_replace("/".$matchtemp[0][0]."/i", $thisvar.",", $temps2);
      }
      if(preg_match_all("(this)", $temps2, $matchtemp)){
      $temps2=preg_replace("/".$matchtemp[0][0]."/i", $thisvar, $temps2);
      }
      $thisvartemp.=$temps2.")";
      }
      $thisvar=$thisvartemp;
      }
      $tempstring.=$thisvar.";";
      $tempstring.=" ?>";
      }


      $replacement[]=$tempstring;
      }
      unset ($v);
     */

    template_arr2patt($all_base[0]);
    $string = preg_replace($all_base[0], $replacement, $string, 1);


    //常量 {C:TPL_DIR}
    $all_base = array();
    preg_match_all("/\{C\:([^\{]*?)\}/is", $string, $all_base);
    if (count($all_base[0]) == 0)
        return $string;

    $replacement = array();
    foreach ($all_base[1] as $k => $v) {
        $replacement[] = "<?php echo ".template_var_call_fun($v)." ; ?>";
    }
    template_arr2patt($all_base[0]);
    $string = preg_replace($all_base[0], $replacement, $string, 1);

    return $string;
}
?>
